const request = require('../../utils/request');

Page({
  data: {
    categories: [],
    dishes: [],
    currentCategory: null
  },

  onLoad() {
    this.loadCategories();
  },

  // 加载分类数据
  async loadCategories() {
    try {
      const res = await request('/api/test/categories');
      this.setData({ 
        categories: res.data,
        currentCategory: res.data[0]?.id
      });
      if (res.data.length > 0) {
        this.loadDishes(res.data[0].id);
      }
    } catch (error) {
      console.error('获取分类失败:', error);
    }
  },

  // 加载菜品数据
  async loadDishes(categoryId) {
    try {
      const res = await request('/api/test/dishes');
      const dishes = res.data.filter(dish => dish.category_id === categoryId);
      this.setData({ dishes });
    } catch (error) {
      console.error('获取菜品失败:', error);
    }
  },

  // 切换分类
  switchCategory(e) {
    const categoryId = e.currentTarget.dataset.id;
    this.setData({ currentCategory: categoryId });
    this.loadDishes(categoryId);
  }
});